package MaterialPack;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Van persie
 */
import Supporter.*;
import javax.microedition.lcdui.*;
import java.util.Vector;
import java.io.IOException;
import javax.microedition.lcdui.game.Sprite;

public class Animation {

  /** 
   * Location to draw the animation, set these fields to
   * change the location where the image is drawn.
   */
  int x, y;

  /**
   * The width and the height of the images (max of all if they
   * are different).
   * They are set when images are loaded and should not be changed.
   */
  int width, height;

  /**
   * Vector of images in the sequence.
   */
  private Vector images;

  /**
   * Current index into the sequence of images.
   */
  private int index;

  /**
   * Size of sequence of images.
   * Set to a large number until the last image of
   * the sequence has been read.
   */
  private int size;
  /**
   * Prefix or name of the image.
   */
  //private String prefix;
  
  private boolean warp = true;

  /**
   * Create a new Animation.
   */
 int nCols = 7;
      
 int nRows = 1;
  public  Animation(){}
  public Animation(int c,int r,int wid,int hei, String Path) {
     // x = xAnch;
     // y = yAnch;
      nCols = c;
      nRows = r;
      warp = true;     
      Load(wid,hei,Path) ;
      //images = new Vector(30);
  }
    public Animation(int c,int r,int wid,int hei, Image image) {
     // x = xAnch;
     // y = yAnch;
        
      nCols = c;
      nRows = r;
      warp = true;        
      Load(wid,hei,image) ;
      //images = new Vector(30);
  }
    public Animation(int wid,int hei, Vector frames) {
      
      width = wid;
      height = hei;
      warp = true; 
      Load(frames);
      index = 0;
  }
    public Image GetCurrentFrame()
    {
        return (Image)images.elementAt(index);
    }
    
 
  public  void next() {
      index = (index + 1);
      if(index == getImages().size())
          if(warp == true)
              index = 0;
      
  }        
   

  public void paint(Graphics g) {
    if (getImages().isEmpty() != true) {
      g.drawImage((Image)getImages().elementAt(index), x, y, Graphics.HCENTER | Graphics.VCENTER);
    }
        
  }
 public void Load(Vector imgs)  {
     images = new Vector();
     for(int i = 0 ; i < imgs.size(); i++)
         images.addElement(ImageSupporter.scaleImage((Image)imgs.elementAt(i), width, height));
  }
 public void Load(int wid,int hei, Image image)  {
     
      images = new Vector();
      
    
        index = 0;
        
        //tempPNG = null;
        
        height = image.getHeight();
        width = image.getWidth();
        int wStep = width/nCols;
        int hStep = height/nRows;
        Image temp;
        
        for(int i = 0; i < nRows; i++)
            for(int j = 0; j < nCols;j++)
            {
                temp = ImageSupporter.scaleImage(Image.createImage(image, j*wStep, i*hStep, wStep, hStep, Sprite.TRANS_NONE),wid,hei);
                getImages().addElement(temp);
            }

  }
  public void Load(int wid,int hei,String pathImg)  {
     
      images = new Vector();
      
    
        index = 0;
        Image image;

        image = ImageSupporter.transparent(pathImg);
        //tempPNG = null;
        
        height = image.getHeight();
        width = image.getWidth();
        int wStep = width/nCols;
        int hStep = height/nRows;
        Image temp;
        
        for(int i = 0; i < nRows; i++)
            for(int j = 0; j < nCols;j++)
            {
                temp = ImageSupporter.scaleImage(Image.createImage(image, j*wStep, i*hStep, wStep, hStep, Sprite.TRANS_NONE),wid,hei);
                getImages().addElement(temp);
            }
//    } catch (IOException ex) {
//      ex.getMessage();
//    } 
  }

  /**
   * Reset the Animation to reduce memory usage.
   * Discard all but the first image.
   */
  void reset() {
    if (getImages().size() > 0) {
      for (int i = 0; i < getImages().size(); i++)
            getImages().setElementAt(null, i);
    }
  }

    /**
     * @return the images
     */
    public Vector getImages() {
        return images;
    }

    /**
     * @param images the images to set
     */
    public void setImages(Vector images) {
        this.images = images;
    }
}
